package easytodo.client;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import easytodo.model.Note;

public class EditNote extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_note);

		titleTextView = (TextView) findViewById(R.id.titleText);
		contentTextView = (TextView) findViewById(R.id.contentText);
		prioritySpinner = (Spinner) findViewById(R.id.prioritySpinner);
		categorySpinner = (Spinner) findViewById(R.id.categorySpinner);

		Button saveButton = (Button) findViewById(R.id.saveButton);
		saveButton.setOnClickListener(SaveNote);

		Button cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(CancelNote);

		ArrayAdapter<CharSequence> categoryAdapter = ArrayAdapter
				.createFromResource(this, R.array.categories,
						android.R.layout.simple_spinner_item);
		categoryAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		categorySpinner.setAdapter(categoryAdapter);

		ArrayAdapter<CharSequence> priorityAdapter = ArrayAdapter
				.createFromResource(this, R.array.priorities,
						android.R.layout.simple_spinner_item);
		priorityAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		prioritySpinner.setAdapter(priorityAdapter);

		Note theNote = (Note) getIntent().getParcelableExtra(
				"easytodo.model.Note");

		if (theNote != null) {
			readValuesFromParcel(theNote);
		} else
			noteID = -1;
	}

	private void readValuesFromParcel(Note note) {
		noteID = note.ID;
		titleTextView.setText(note.Title);
		contentTextView.setText(note.Content);
		prioritySpinner.setSelection(note.PriorityValue);
		categorySpinner.setSelection(note.CategoryID);
	}

	private OnClickListener SaveNote = new OnClickListener() {
		public void onClick(View v) {
			Intent resultIntent = new Intent();
			Note theNote = readValuesFromView();
			resultIntent.putExtra("easytodo.model.Note", theNote);
			setResult(Activity.RESULT_OK, resultIntent);
			finish();
		}
	};

	private Note readValuesFromView() {
		//TODO get real priority and category ID.
		Note theNote = new Note();
		theNote.Title = titleTextView.getText();
		theNote.Content = contentTextView.getText();
		theNote.PriorityValue = prioritySpinner.getSelectedItemPosition();
		theNote.CategoryID = categorySpinner.getSelectedItemPosition();
		
		if (noteID != -1)
			theNote.ID = noteID;

		return theNote;
	}

	private OnClickListener CancelNote = new OnClickListener() {
		public void onClick(View v) {
			Intent resultIntent = new Intent();
			setResult(Activity.RESULT_CANCELED, resultIntent);
			finish();
		}
	};

	private int noteID;
	private TextView titleTextView;
	private TextView contentTextView;
	private Spinner prioritySpinner;
	private Spinner categorySpinner;
}
